<?php

class DomainsController extends AdminController
{
    // 节点列表
    public function listAction()
    {
        $do = $this->get('do');
        switch ($do) {
            case 'list':
                $parameters['order'] = 'id desc';
                $parameters['conditions'] = [];
                $groupid = intval($this->get('groupid'));
                if ($groupid > 0) {
                    $parameters['conditions'][] = ['groupid', $groupid];
                }
                $DomainName = trim($this->get('DomainName'));
                if ($DomainName ) {
                    $parameters['conditions'][] = ['DomainName','like', "%{$DomainName}%"];
                }
                $data = $this->_list(DunDomainsModel::class, $parameters);
                foreach ($data['data'] as $item) {
                    $data_result[] = array(
                        'id' => $item->id,
                        'account' => $item->group->account,
                        'groupid' => $item->groupid,
                        'DomainId' => $item->DomainId,
                        'DomainName' => $item->DomainName,
                        'RecordCount' => $item->RecordCount,
                        'AllData' => $item->AllData,
                    );
                }
                $this->ajaxresult(0, '', $data_result, array('count' => $data['count']));
                break;
        }
        // 加载所有账户
        $this->view->assign('all_groups', DunGroupsModel::select());
    }

    // 添加解析
    function setAction()
    {
        $DomainName = trim($this->get('DomainName'));
        $groupid = trim($this->get('groupid'));
        if ($this->ispost) {
            $DomainName = trim($this->post('DomainName'));
            $groupid = trim($this->post('groupid'));
            $Value = $this->post('Value');
            $Type = $this->post('Type');
            $RR = $this->post('RR');
            $DomainRecordInfo = [];
            foreach ($Value as $k => $v) {
                if (!empty($v)) {
                    $i = $k + 1;
                    $DomainRecordInfo["DomainRecordInfo.{$i}.Domain"] = $DomainName;
                    $DomainRecordInfo["DomainRecordInfo.{$i}.Type"] = $Type;
                    $DomainRecordInfo["DomainRecordInfo.{$i}.Rr"] = $RR;
                    $DomainRecordInfo["DomainRecordInfo.{$i}.Value"] = $v;
                };
            }
            if ($DomainRecordInfo) {
                $account = DunGroupsModel::find($groupid);
                $regionid = 'cn-hangzhou';
                if ($account && $regionid) {
                    $alibaba = new Aliyun($regionid, $account->accessKeyId, $account->accessSecret);
                    $alibaba->setHost('Alidns');
                    $alibaba->OperateBatchDomain('RR_ADD', $DomainRecordInfo);
                    $this->ajaxresult(0, '操作成功');
                }
            }
        }
        $this->view->assign('DomainName', $DomainName);
        $this->view->assign('groupid', $groupid);
    }

    // 批量解析
    function setsAction()
    {

    }

    // 解析记录
    function recordAction()
    {
        $DomainName = trim($this->get('DomainName'));
        $groupid = trim($this->get('groupid'));
        $do = $this->get('do');
        switch ($do) {
            case 'list':
                $account = DunGroupsModel::find($groupid);
                $regionid = 'cn-hangzhou';
                if ($account && $regionid) {
                    $alibaba = new Aliyun($regionid, $account->accessKeyId, $account->accessSecret);
                    $alibaba->setHost('Alidns');
                    $alidata = $alibaba->DescribeDomainRecords($DomainName, $this->page, $this->page_size);
                    if ($alidata && $alidata['TotalCount'] > 0) {
                        foreach ($alidata['DomainRecords']['Record'] as $item) {
                            $data_result[] = array(
                                'RecordId' => $item['RecordId'],
                                'RR' => $item['RR'],
                                'Type' => $item['Type'],
                                'Value' => $item['Value'],
                                'Line' => $item['Line'],
                                'Priority' => $item['Priority'],
                                'TTL' => $item['TTL'],
                                'Status' => $item['Status'],
                                'Locked' => $item['Locked'],
                                'DomainName' => $item['DomainName'],
                            );
                        }
                    }
                }
                $this->ajaxresult(0, '', $data_result, array('count' => $alidata['TotalCount'] ?? 0));
                break;
        }
        $this->view->assign('DomainName', $DomainName);
        $this->view->assign('groupid', $groupid);
    }

    // 删除解析
    function delrecordAction()
    {
        if ($this->ispost) {
            $groupid = $this->post('groupid');
            $RecordId = $this->post('RecordId');
            $account = DunGroupsModel::find($groupid);
            $regionid = 'cn-hangzhou';
            if ($account && $regionid) {
                $alibaba = new Aliyun($regionid, $account->accessKeyId, $account->accessSecret);
                $alibaba->setHost('Alidns');
                $alibaba->DeleteDomainRecord($RecordId);
                $this->ajaxresult(0, '处理成功');
            }
            $this->ajaxresult(1, '阿里云账户有误');
        }
        $this->ajaxresult();
    }

    // 更新
    function refreshAction()
    {
        if ($this->ispost) {
            $groupid = $this->post('groupid');
            $regionid = $this->post('regionid');
            $account = DunGroupsModel::find($groupid);
            if ($account && $regionid) {
                $alibaba = new Aliyun($regionid, $account->accessKeyId, $account->accessSecret);
                $alibaba->setHost('Alidns');
                $alidata = $alibaba->DescribeDomains();
                if ($alidata && $alidata['TotalCount'] > 0) {
                    foreach ($alidata['Domains']['Domain'] as $item) {
                        $ecs = DunDomainsModel::where([
                            'DomainId' => $item['DomainId'],
                            'groupid' => $groupid
                        ])->find();
                        $data = [
                            'groupid' => $account->id,
                            'DomainId' => $item['DomainId'],
                            'DomainName' => $item['DomainName'],
                            'RecordCount' => $item['RecordCount'],
//                            'InBlackHole' => $item['InBlackHole'],
//                            'InClean' => $item['InClean'],
                            'AllData' => json_encode($item)
                        ];
                        if ($ecs) {
                            $ecs->save($data);
                        } else {
                            DunDomainsModel::create($data);
                        }
                    }
                }
                $this->ajaxresult(0, '操作成功');
            }
            $this->ajaxresult(1, '账户不存在');
        }
        // 加载所有地区
        $this->view->assign('all_regionid', Aliyun::getConfig('Alidns'));
        // 加载所有账户
        $this->view->assign('all_account', DunGroupsModel::select());
    }
}